#include "window.h"

#include "gl_triangle_mesh.h"
#include "node.h"

eqtest::Window::Window(eq::Pipe* parent):
eq::Window(parent)
{
}

bool eqtest::Window::configInitGL(const eq::uint128_t& initID)
{
    if(!eq::Window::configInitGL(initID))
        return false;
    
    Node *node = dynamic_cast<Node*>(this->getNode());
    
    mesh.reset(new GLTriangleMesh(node->getObjFile(), this->glewGetContext()));
    
    texAlbedo  .reset(new GLImageTexture(node->getAlbedo()  , this->glewGetContext()));
    texNormal  .reset(new GLImageTexture(node->getNormal()  , this->glewGetContext()));
    texSpecular.reset(new GLImageTexture(node->getSpecular(), this->glewGetContext()));
    texStretch .reset(new GLImageTexture(node->getStretch() , this->glewGetContext()));
    texRho_d   .reset(new GLImageTexture(node->getRho_d()   , this->glewGetContext()));
    
    shader.reset(new ShaderProgram("shaders/final.shv", "shaders/texmap.shf", this->glewGetContext()));
    
    if(!shader->success())
        throw std::runtime_error(shader->log());
    
    shader->bind();
    
    glUniform1i(shader->uniform("albedoTex")  , 0);
    glUniform1i(shader->uniform("normalTex")  , 1);
    glUniform1i(shader->uniform("specularTex"), 2);
    glUniform1i(shader->uniform("rho_dTex")   , 4);
    
    return true;
}

bool eqtest::Window::configExitGL()
{
    shader.reset();
    mesh.reset();
    
    return eq::Window::configExitGL();
}
